<template>
  <el-card>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="ruleForm.type">
          <el-radio :label="1">目录</el-radio>
          <el-radio :label="2">菜单</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="ruleForm.type==2" label="上级" prop="pid">
        <el-select v-model="ruleForm.pid" placeholder="请选择">
          <el-option v-for="item in topMenu" :key="item.id" :label="item.title" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input type="text" v-model="ruleForm.title" autocomplete="off" placeholder="请输入菜单标题"></el-input>
      </el-form-item>
      <el-form-item v-if="ruleForm.type==2" label="地址" prop="url">
        <el-input type="text" v-model="ruleForm.url" autocomplete="off" placeholder="请输入菜单地址"></el-input>
      </el-form-item>
      <el-form-item v-if="ruleForm.type==1" label="图标" prop="icon">
        <el-input v-model.number="ruleForm.icon" placeholder="请输入菜单图标"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="ruleForm.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      // 顶级菜单: 需要请求数据接口进行获取
      topMenu: [],
      ruleForm: {
        id: this.$route.params.id,
        type: 1,
        title: "",
        icon: "",
        status: 1,
        pid: 0
      },
      rules: {
        type: [{ required: true, message: "请选择菜单类型", trigger: "blur" }],
        title: [{ required: true, message: "请输入菜单标题", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }]
      }
    };
  },
  created() {
    // 获取顶级菜单
    this.getTopMenu();
    // 获取当前菜单信息
    this.getMenuInfo();
  },
  methods: {
    // 获取当前菜单的信息
    getMenuInfo() {
      this.$http.get(`/api/menuinfo?id=${this.ruleForm.id}`).then(res => {
        if (res.code == 200) {
          this.ruleForm = res.list;
        }
      });
    },
    // 获取顶级菜单
    getTopMenu() {
      this.$http.get("/api/menulist?istree=1").then(res => {
        if (res.code == 200) {
          this.topMenu = res.list;
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 请求数据接口
          this.$http.post("/api/menuedit",{...this.ruleForm,id:this.$route.params.id}).then(res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "修改成功"
              });
              return ;
            }
            this.$message({
              type: "error",
              message: "修改失败"
            });
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style>
.demo-ruleForm {
  width: 600px;
  margin: auto;
}
</style>